import config from "../config/config";
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import func from "../config/func";
import AdManage from "../manage/AdManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class VivoNativeAd extends cc.Component {

    @property(cc.Sprite)
    bg: cc.Sprite = null
    @property(cc.Label)
    tltle: cc.Label = null
    @property(cc.Label)
    btnCont: cc.Label = null;
    @property(cc.Node)
    btnClose: cc.Node = null;

    n_ad: any = null;
    onLoad(){
        this.bg.node.on(cc.Node.EventType.TOUCH_END, this.clickBg, this);
    }

    init() {
        config.openPopup = false;
        func.creatrBanner();
        if(config.qg_loadCfg && config.qg_loadCfg.zs_native_touch_switch == 1){
            this.btnClose.width = 32;
            this.btnClose.height = 32;
        }else{
            this.btnClose.width = 35;
            this.btnClose.height = 35;
        }


        let id = config.vivoCustomAd;
        let num = Math.random();
        if(num < 0.5){
            id = config.vivoCustomAd2;
        }
        let self = this;
        AdManage.instance.initCustomAd(id, this.node, (res: any, ad: any) => {
            if (res !== 0) {
                self.tltle.string = res.desc;
                cc.assetManager.loadRemote(res.imgUrlList[0], (err: any, texture: cc.Texture2D) => {
                    if (err) {
                        console.log(JSON.stringify(err), "原生广告图片加载失败");
                    } else {
                        self.bg.spriteFrame = new cc.SpriteFrame(texture);
                    }
                })
            }else{
                self.setClose();
            }
        })
        if (config.qg_loadCfg && config.qg_loadCfg.zs_version == config.vivo_zs_version && config.qg_loadCfg.zs_jump_switch == 1) {
            this.btnCont.string = "点击查看";
            // this.bg.node.on(cc.Node.EventType.TOUCH_END, this.click, this);
        } else {
            this.btnCont.string = "点击跳过";
        }
    }

    clickBg(){
        if (config.qg_createNativeAd != null) {
            AdManage.instance.n_CustomAd.reportAdClick({
                adId: config.qg_createNativeAd.adId
            })
        }
        this.setClose();
    }

    click() {
        if (config.qg_loadCfg && config.qg_loadCfg.zs_version == config.vivo_zs_version && config.qg_loadCfg.zs_native_click_switch == 1) {
            this.clickBg();
        }
        this.setClose();
    }
    close() {
        // if (config.qg_loadCfg && config.qg_loadCfg.zs_version == config.vivo_zs_version && config.qg_loadCfg.zs_native_click_switch == 1) {
        //     this.clickBg()
        // }
        this.setClose();
    }

    setClose(){
        this.node.active = false;
        func.destroyBanner();
        this.node.destroy();
    }
}
